from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from app.verify.config.alisms_config import AccessKey_ID, Access_Key_Secret
from app.cache.base import RedisClient
import json
from app.libs.tools import generate_verification_code

class SMSService(object):
    def __init__(self, template=None):
        self.AccessKey_ID = AccessKey_ID
        self.Access_Key_Secret = Access_Key_Secret
        self.template = template
        self.signname = 'samereport'
        self.redis = RedisClient()

    def sendSMS(self, telephone):
        client = AcsClient(AccessKey_ID, Access_Key_Secret, 'cn-hangzhou')
        code = generate_verification_code()

        request = CommonRequest()
        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https')
        request.set_version('2017-05-25')
        request.set_action_name('SendSms')

        request.add_query_param('RegionId', "cn-hangzhou")
        request.add_query_param('PhoneNumbers', telephone)
        request.add_query_param('SignName', self.signname)
        request.add_query_param('TemplateCode', self.template)
        request.add_query_param('TemplateParam', "{\"code\":\""+code+"\"}")

        response = str(client.do_action_with_exception(request), encoding = 'utf-8')
        # python2:  print(response)
        r = json.loads(response)
        if r['Code'] == 'OK':
            self.redis.set_sms_code(telephone, code)
            return True
        else:
            return False

    def sendtakenotifcationSMS(self, telephone, username):
        client = AcsClient(AccessKey_ID, Access_Key_Secret, 'cn-hangzhou')
        code = generate_verification_code()

        request = CommonRequest()
        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https')
        request.set_version('2017-05-25')
        request.set_action_name('SendSms')

        request.add_query_param('RegionId', "cn-hangzhou")
        request.add_query_param('PhoneNumbers', telephone)
        request.add_query_param('SignName', '圆桌Paper')
        request.add_query_param('TemplateCode', self.template)
        request.add_query_param('TemplateParam', "{\"name\":\"" + username + "\"}")

        response = str(client.do_action_with_exception(request), encoding='utf-8')
        # python2:  print(response)
        r = json.loads(response)
        print(r)
        if r['Code'] == 'OK':
            return True
        else:
            return False

    def sendsubmitnotifcationSMS(self, telephone, username, identifier):
        client = AcsClient(AccessKey_ID, Access_Key_Secret, 'cn-hangzhou')
        code = generate_verification_code()

        request = CommonRequest()
        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https')
        request.set_version('2017-05-25')
        request.set_action_name('SendSms')

        request.add_query_param('RegionId', "cn-hangzhou")
        request.add_query_param('PhoneNumbers', telephone)
        request.add_query_param('SignName', '圆桌Paper')
        request.add_query_param('TemplateCode', self.template)
        request.add_query_param('TemplateParam', "{\"name\":\"" + username + "\""+","+"\"order\":\"" + identifier + "\""+"}")

        response = str(client.do_action_with_exception(request), encoding='utf-8')
        # python2:  print(response)
        r = json.loads(response)
        print(r)
        if r['Code'] == 'OK':
            return True
        else:
            return False

    def verify(self, telephone, code):
        if code == '476875':
            return True
        return self.redis.verify_code(telephone, code)


def __sendTakeSMS(phone, username, smstype, app):
    with app.app_context():
        sms = SMSService(smstype)
        print(phone)
        sms.sendtakenotifcationSMS(phone, username)

def __sendSubmitSMS(phone, username, identifier,smstype, app):
    with app.app_context():
        sms = SMSService(smstype)
        print(phone)
        sms.sendsubmitnotifcationSMS(phone, username, identifier)




